package com.example.demo.service.impl;

import cn.hutool.core.io.FileUtil;
import com.example.demo.entity.*;
import com.example.demo.service.ActLeaveService;
import com.google.common.collect.Maps;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

@Service
public class ActLeaveServiceImpl implements ActLeaveService {
    @Value("${file.path}")
    private String filePath;

    public void exportToPdf(BdActLeave bdActLeave, HttpServletResponse response) throws DocumentException, IOException {
        Map<String, Object> paramsa =Maps.newHashMap();
        paramsa.put("groundProtection1", bdActLeave.getGroundProtection1());
        paramsa.put("groundProtection2", bdActLeave.getGroundProtection2());
        paramsa.put("groundProtection3", bdActLeave.getGroundProtection3());
        paramsa.put("groundProtection4", bdActLeave.getGroundProtection4());
        paramsa.put("groundProtection5", bdActLeave.getGroundProtection5());
        paramsa.put("groundProtectionNotes", bdActLeave.getGroundProtectionNotes());
        paramsa.put("decorationProtection1", bdActLeave.getDecorationProtection1());
        paramsa.put("decorationProtection2", bdActLeave.getDecorationProtection2());
        paramsa.put("decorationProtection3", bdActLeave.getDecorationProtection3());
        paramsa.put("decorationProtection4", bdActLeave.getDecorationProtection4());
        paramsa.put("decorationProtection5", bdActLeave.getDecorationProtection5());
        paramsa.put("decorationProtection6", bdActLeave.getDecorationProtection6());
        paramsa.put("decorationProtection7", bdActLeave.getDecorationProtection7());
        paramsa.put("decorationProtection8", bdActLeave.getDecorationProtection8());
        paramsa.put("decorationProtectionNotes", bdActLeave.getDecorationProtectionNotes());
        paramsa.put("pollutionControl1", bdActLeave.getPollutionControl1());
        paramsa.put("pollutionControl2", bdActLeave.getPollutionControl2());
        paramsa.put("pollutionControl3", bdActLeave.getPollutionControl3());
        paramsa.put("pollutionControl4", bdActLeave.getPollutionControl4());
        paramsa.put("pollutionControlNotes", bdActLeave.getPollutionControlNotes());
        paramsa.put("constructionWork1", bdActLeave.getConstructionWork1());
        paramsa.put("constructionWork2", bdActLeave.getConstructionWork2());
        paramsa.put("constructionWork3", bdActLeave.getConstructionWork3());
        paramsa.put("constructionWorkNotes", bdActLeave.getConstructionWorkNotes());
        paramsa.put("electrificationWork1", bdActLeave.getElectrificationWork1());
        paramsa.put("electrificationWork2", bdActLeave.getElectrificationWork2());
        paramsa.put("electrificationWork3", bdActLeave.getElectrificationWork3());
        paramsa.put("electrificationWork4", bdActLeave.getElectrificationWork4());
        paramsa.put("electrificationWork5", bdActLeave.getElectrificationWork5());
        paramsa.put("electrificationWorkNotes", bdActLeave.getElectrificationWorkNotes());
        paramsa.put("otherWork1", bdActLeave.getOtherWork1());
        paramsa.put("otherWork2", bdActLeave.getOtherWork2());
        paramsa.put("otherWork3", bdActLeave.getOtherWork3());
        paramsa.put("otherWork4", bdActLeave.getOtherWork4());
        paramsa.put("otherWork5", bdActLeave.getOtherWork5());
        paramsa.put("otherWork6", bdActLeave.getOtherWork6());
        paramsa.put("otherWorkNotes", bdActLeave.getOtherWorkNotes());
        paramsa.put("otherProblems", bdActLeave.getOtherProblems());
        paramsa.put("pollution1", bdActLeave.getPollution1());
        paramsa.put("pollution2", bdActLeave.getPollution2());
        paramsa.put("pollution3", bdActLeave.getPollution3());
        paramsa.put("pollution4", bdActLeave.getPollution4());
        paramsa.put("pollution5", bdActLeave.getPollution5());
        paramsa.put("pollution6", bdActLeave.getPollution6());
        paramsa.put("pollutionNotes", bdActLeave.getPollutionNotes());
        paramsa.put("siteDamage1", bdActLeave.getSiteDamage1());
        paramsa.put("siteDamage1Area", bdActLeave.getSiteDamage1Area());
        paramsa.put("siteDamage1Reason", bdActLeave.getSiteDamage1Reason());
        paramsa.put("siteDamage2", bdActLeave.getSiteDamage2());
        paramsa.put("siteDamage2Area", bdActLeave.getSiteDamage2Area());
        paramsa.put("siteDamage2Reason", bdActLeave.getSiteDamage2Reason());
        paramsa.put("siteDamage3", bdActLeave.getSiteDamage3());
        paramsa.put("siteDamage3Area", bdActLeave.getSiteDamage3Area());
        paramsa.put("siteDamage3Reason", bdActLeave.getSiteDamage3Reason());
        paramsa.put("siteDamage4", bdActLeave.getSiteDamage4());
        paramsa.put("siteDamage4Area", bdActLeave.getSiteDamage4Area());
        paramsa.put("siteDamage4Reason", bdActLeave.getSiteDamage4Reason());
        paramsa.put("siteDamage5", bdActLeave.getSiteDamage5());
        paramsa.put("siteDamage5Area", bdActLeave.getSiteDamage5Area());
        paramsa.put("siteDamage5Reason", bdActLeave.getSiteDamage5Reason());
        paramsa.put("siteDamage6", bdActLeave.getSiteDamage6());
        paramsa.put("siteDamage6Area", bdActLeave.getSiteDamage6Area());
        paramsa.put("siteDamage6Reason", bdActLeave.getSiteDamage6Reason());
        paramsa.put("siteDamage7", bdActLeave.getSiteDamage7());
        paramsa.put("siteDamage7Area", bdActLeave.getSiteDamage7Area());
        paramsa.put("siteDamage7Reason", bdActLeave.getSiteDamage7Reason());
        paramsa.put("siteDamage8", bdActLeave.getSiteDamage8());
        paramsa.put("siteDamage8Area", bdActLeave.getSiteDamage8Area());
        paramsa.put("siteDamage8Reason", bdActLeave.getSiteDamage8Reason());
        paramsa.put("siteDamage9", bdActLeave.getSiteDamage9());
        paramsa.put("siteDamage9Area", bdActLeave.getSiteDamage9Area());
        paramsa.put("siteDamage9Reason", bdActLeave.getSiteDamage9Reason());
        paramsa.put("siteDamage10", bdActLeave.getSiteDamage10());
        paramsa.put("siteDamage10Area", bdActLeave.getSiteDamage10Area());
        paramsa.put("siteDamage10Reason", bdActLeave.getSiteDamage10Reason());
        paramsa.put("siteDamage11", bdActLeave.getSiteDamage11());
        paramsa.put("siteDamage11Area", bdActLeave.getSiteDamage11Area());
        paramsa.put("siteDamage11Reason", bdActLeave.getSiteDamage11Reason());
        paramsa.put("sitePollution1", bdActLeave.getSitePollution1());
        paramsa.put("sitePollution1Area", bdActLeave.getSitePollution1Area());
        paramsa.put("sitePollution1Reason", bdActLeave.getSitePollution1Reason());
        paramsa.put("sitePollution2", bdActLeave.getSitePollution2());
        paramsa.put("sitePollution2Area", bdActLeave.getSitePollution2Area());
        paramsa.put("sitePollution2Reason", bdActLeave.getSitePollution2Reason());
        paramsa.put("sitePollution3", bdActLeave.getSitePollution3());
        paramsa.put("sitePollution3Area", bdActLeave.getSitePollution3Area());
        paramsa.put("sitePollution3Reason", bdActLeave.getSitePollution3Reason());
        paramsa.put("sitePollution4", bdActLeave.getSitePollution4());
        paramsa.put("sitePollution4Area", bdActLeave.getSitePollution4Area());
        paramsa.put("sitePollution4Reason", bdActLeave.getSitePollution4Reason());
        paramsa.put("sitePollution5", bdActLeave.getSitePollution5());
        paramsa.put("sitePollution5Area", bdActLeave.getSitePollution5Area());
        paramsa.put("sitePollution5Reason", bdActLeave.getSitePollution5Reason());
        paramsa.put("sitePollution6", bdActLeave.getSitePollution6());
        paramsa.put("sitePollution6Area", bdActLeave.getSitePollution6Area());
        paramsa.put("sitePollution6Reason", bdActLeave.getSitePollution6Reason());
        paramsa.put("deductionConfirmation", bdActLeave.getDeductionConfirmation());
        paramsa.put("waste", bdActLeave.getWaste());
        paramsa.put("wasteNotes", bdActLeave.getWasteNotes());
        paramsa.put("inspectionFee", bdActLeave.getInspectionFee());
        paramsa.put("truckDeparture1", bdActLeave.getTruckDeparture1());
        paramsa.put("truckDeparture2", bdActLeave.getTruckDeparture2());
       // 字体样式
        BaseFont bf = BaseFont.createFont("c:/windows/fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        ArrayList<BaseFont> fontList = new ArrayList<>();
        fontList.add(bf);

        // 利用模板生成pdf
        String templatePath = filePath+"leaveTemplate.pdf";

        // 生成的新文件路径
        String caseId = "撤场单"+ bdActLeave.getId().toString()+".pdf";
        String newPDFPath = "D:\\system\\system-back\\files\\pdf\\" + caseId;

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            form.setSubstitutionFonts(fontList);
            for (String key : paramsa.keySet()) {
                if (paramsa.get(key)==null){
                    paramsa.put(key,"");
                }
                form.setSubstitutionFonts(fontList);
                form.setField(key,paramsa.get(key).toString(),true);
            }
            stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            copy.open();
            doc.open();
//            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
//            copy.addPage(importPage);
            PdfImportedPage importPage=null;
            for (int i=1;i<=reader.getNumberOfPages();i++){
                importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            copy.close();
            doc.close();
            out.close();
        } catch (IOException | DocumentException e) {
            throw new RuntimeException(e);
        }

        try {
            OutputStream os;  // 新建一个输出流对象
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(caseId, "UTF-8"));
            response.setContentType("application/octet-stream");
            byte[] bytes = FileUtil.readBytes(newPDFPath);  // 通过文件的路径读取文件字节流
            os = response.getOutputStream();   // 通过输出流返回文件
            os.write(bytes);
            os.flush();
            os.close();
        } catch (Exception e) {
            System.out.println("文件下载失败");
        }
    }


}
